Skip to content

[Frontend] Axios Interceptor for Auth Tokens#314

Open
armorbreak001 wants to merge 1 commit intoGalactiGuild:mainfrom
armorbreak001:bounty/287-axios-interceptor
Open

[Frontend] Axios Interceptor for Auth Tokens#314
armorbreak001 wants to merge 1 commit intoGalactiGuild:mainfrom
armorbreak001:bounty/287-axios-interceptor

Conversation

@armorbreak001
Copy link
Copy Markdown

Fixes #287

Changes

  • src/lib/api/client.ts: Pre-configured Axios instance with dual interceptors
    • Request interceptor: Reads JWT from localStorage.getItem('stellar_auth_token') and attaches Authorization: Bearer <token> to every request
    • Response interceptor:
      • Catches 401 Unauthorized globally
      • Attempts token refresh via POST /auth/refresh (if refresh token exists)
      • Queues concurrent requests during refresh (prevents race conditions)
      • On refresh failure: calls clearAuthState() which removes token + dispatches 'auth:logout' custom event for app-wide listeners
    • Configurable base URL via NEXT_PUBLIC_API_URL env var, 30s timeout
  • package.json: Added axios dependency

Acceptance Criteria Met

  • Create src/lib/api/client.ts exporting a configured Axios instance
  • Request interceptor pulls JWT from localStorage and attaches to headers
  • Response interceptor catches 401 errors and triggers forced global logout event

How to Verify

  1. Set a JWT in localStorage: localStorage.setItem('stellar_auth_token', 'test-token')
  2. Import: import apiClient from '@/lib/api/client';
  3. Call apiClient.get('/users/me') → request includes Authorization: Bearer test-token header
  4. On 401 response → interceptor clears token + dispatches auth:logout event

- Create apiClient with request interceptor for automatic JWT attachment
- Response interceptor catches 401 errors and triggers global logout
- Clears localStorage tokens and dispatches 'auth:logout' custom event
- Redirects to /login on 401 (with loop prevention)
- Exports typed convenience wrappers: api.get/post/put/patch/delete
- Reads token from localStorage (auth_token or jwt key)
@armorbreak001 armorbreak001 force-pushed the bounty/287-axios-interceptor branch from 5a05631 to 9074bb4 Compare April 14, 2026 13:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Frontend] Axios Interceptor for Auth Tokens

1 participant